﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using SXFramWork.Core;
using SXFramWork.Service;

namespace SXFramWork.API.Controllers.Admin
{
    /// <summary>
    /// 权限管理
    /// </summary>
    public class SysRolePermissionController : WebController
    {
        private readonly ISysRolePermissionService _service;
        public SysRolePermissionController(ISysRolePermissionService sysRolePermissionService)
        {
            _service = sysRolePermissionService;
        }
        /// <summary>
        /// 权限管理页面 说明：这里仅仅是为了方便创建菜单，省去了在页面去创建菜单
        /// </summary>
        /// <returns></returns>
        [Menu("权限管理")]
        [HttpGet]
        public async Task<IApiResult> Setting()
        {
            return ApiResult.Ok();
        }
        /// <summary>
        /// 查询角色权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        [Power("查询角色权限")]
        public async Task<IApiResult> GetPermission([FromQuery] string roleId )
        {
            return await _service.QueryPermission(roleId);
        }
        /// <summary>
        /// 角色授权
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Power("授权")]
        public async Task<IApiResult> Add([FromBody] JObject obj)
        {
            JArray arr =obj.Value<JArray>("permissionIds");
            var list= arr.ToObject<List<string>>();
          
            return await _service.Add(list,obj.Value<string>("roleId"));
        }

        /// <summary>
        /// 查询权限列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Power("查询角色列表")]
        public async Task<IApiResult> GetPermissionList()
        {
            return await _service.GetPermissionList();
        }
    }
}